<template>
  <div class="matching-view">
    <!-- 匹配头部 -->
    <div class="app-header">
      <div class="header-container">
        <button class="back-button" @click="cancelMatching">
          <i class="fas fa-chevron-left"></i>
        </button>
        <div class="header-title">匹配中</div>
      </div>
      <div class="progress-container">
        <div class="progress-bar" :style="{ width: progress + '%' }"></div>
      </div>
    </div>

    <!-- 匹配内容 -->
    <div class="matching-content">
      <div class="avatar-circle">
        <div class="avatar-pulse"></div>
        <div class="avatar">
          <i class="fas fa-user"></i>
        </div>
      </div>

      <div class="matching-text">
        <h2>正在寻找匹配对象...</h2>
        <p>我们会为您寻找最合适的聊天伙伴</p>
      </div>

      <div class="counter-container">
        <span class="counter">{{ counter }}</span>
        <span>秒</span>
      </div>
    </div>

    <!-- 取消按钮 -->
    <div class="action-area">
      <button class="cancel-button" @click="cancelMatching">取消匹配</button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from "vue";
import { useRouter } from "vue-router";

const router = useRouter();
const counter = ref(0);
const progress = ref(0);
const intervalId = ref<number>();

// 开始匹配计时器
onMounted(() => {
  let elapsed = 0;
  const maxTime = 90; // 最大匹配时间90秒

  intervalId.value = window.setInterval(() => {
    elapsed++;
    counter.value = elapsed;
    progress.value = (elapsed / maxTime) * 100;

    // 模拟匹配成功
    if (elapsed > 5) {
      finishMatching();
    }

    // 超过最大时间停止匹配
    if (elapsed >= maxTime) {
      finishMatching();
    }
  }, 1000);
});

// 停止匹配
const finishMatching = () => {
  if (intervalId.value) {
    clearInterval(intervalId.value);
    intervalId.value = undefined;
  }
};

// 取消匹配
const cancelMatching = () => {
  if (intervalId.value) {
    clearInterval(intervalId.value);
    intervalId.value = undefined;
  }
  router.back();
};

// 组件卸载时清理定时器
onUnmounted(() => {
  if (intervalId.value) {
    clearInterval(intervalId.value);
  }
});
</script>

<style scoped>
.matching-view {
  background: linear-gradient(135deg, #3498db, #8e44ad);
  min-height: 100vh;
  color: white;
  display: flex;
  flex-direction: column;
}

.app-header {
  padding: 16px;
  position: relative;
}

.header-container {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
}

.back-button {
  background: none;
  border: none;
  color: white;
  font-size: 16px;
  padding: 8px;
  margin-right: 12px;
  border-radius: 50%;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.3s;
}

.back-button:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.header-title {
  font-size: 18px;
  font-weight: 600;
  flex-grow: 1;
}

.progress-container {
  height: 4px;
  background-color: rgba(255, 255, 255, 0.2);
  border-radius: 2px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  background-color: white;
  border-radius: 2px;
  transition: width 0.5s ease;
}

.matching-content {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0 20px;
  text-align: center;
}

.avatar-circle {
  position: relative;
  width: 180px;
  height: 180px;
  margin-bottom: 40px;
}

.avatar {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 120px;
  height: 120px;
  background-color: #2980b9;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 48px;
  z-index: 2;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
}

.avatar-pulse {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.1);
  animation: pulse 2s infinite;
  z-index: 1;
}

@keyframes pulse {
  0% {
    transform: scale(0.8);
    opacity: 0.7;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.3;
  }
  100% {
    transform: scale(1.5);
    opacity: 0;
  }
}

.matching-text h2 {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 8px;
}

.matching-text p {
  font-size: 16px;
  opacity: 0.9;
  margin-bottom: 40px;
}

.counter-container {
  font-size: 24px;
  font-weight: 600;
  display: flex;
  align-items: baseline;
  margin-top: 20px;
}

.counter {
  font-size: 48px;
  margin: 0 8px;
}

.action-area {
  padding: 24px;
}

.cancel-button {
  background: rgba(255, 255, 255, 0.15);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 24px;
  padding: 14px 32px;
  font-size: 16px;
  font-weight: 500;
  width: 100%;
  transition: background-color 0.3s;
}

.cancel-button:hover {
  background: rgba(255, 255, 255, 0.25);
}
</style>
